package cn.hs.controller;

import cn.hs.entity.Consult;
import cn.hs.mapper.ConsultMapper;
import cn.hs.service.ConsultService;
import cn.hs.util.Result;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName ConsultController
 * @Description TODO
 * @Author yyk
 * @Date 2024年10月23日 20:00
 * @Version 1.0
 */

@RestController
@RequestMapping("/ConsultController")
public class ConsultController {
    @Autowired
    ConsultService consultService;

   @RequestMapping("/getConsultList")
    public Result getConsultList(Integer id,Integer levelId, String name,@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "5") int pageSize) {
       Consult consult = new Consult(name);
       consult.setLevelId(levelId);
       consult.setId(id);

       PageInfo consultList = consultService.getConsultList(consult,  pageNum, pageSize);
       if (consultList == null || consultList.getTotal() == 0) {
           return Result.error();
       }else {
           return Result.success(consultList);
       }

   }

   @PostMapping("/insert")
    public Result insert(@RequestBody Consult consult) {
       consultService.insert(consult);
       if (consult.getId() != null) {
           return Result.success();
       }else {
           return Result.error();
       }
   }

   @PostMapping("/updateByPrimaryKey")
    public Result updateByPrimaryKey(@RequestBody Consult consult) {
       int i = consultService.updateByPrimaryKey(consult);
       if (i > 0) {
           return Result.success();
       }else {
           return Result.error();
       }
   }

   @GetMapping("/deleteByPrimaryKey")
    public Result deleteByPrimaryKey(@RequestParam Integer id) {
       int i = consultService.deleteByPrimaryKey(id);
       if (i > 0) {
           return Result.success();
       }else {
           return Result.error();
       }
   }

   @GetMapping("/findById")
    public Result findById(@RequestParam Integer id,@RequestParam(defaultValue = "1") int pageNum,@RequestParam(defaultValue = "5") int pageSize) {
       PageInfo byId = consultService.findById(id, pageNum, pageSize);
      if (byId == null || byId.getTotal() == 0) {
          return Result.error();
      }else {
          return Result.success(byId);
      }
   }


   @GetMapping("/getConsultList2")
    public Result getConsultList2(Integer id,String name,@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "5") int pageSize) {

       Consult consult = new Consult();
       consult.setName(name);
       consult.setId(id);
       PageInfo consultList = consultService.getConsultList2(consult,  pageNum, pageSize);
       if (consultList == null || consultList.getTotal() == 0) {
           return Result.error();
       }else {
           return Result.success(consultList);
       }
   }

}
